﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Microsoft.Practices.Unity;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCE.UI.Blocks;
using Baosight.ZNH.ZMCE.UI.PopWindows;

namespace Baosight.ZNH.ZMCE.UI
{
    public partial class ZMCE31 : EFViewCmes
    {
        private string actionType;
        public ZMCE31()
        {
            InitializeComponent();
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "设备变动-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            this.actionType = "update";
            base.OnUpdateButtonClickPopup(eiInfo, "设备变动-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 删除按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            this.actionType = "delete";
            base.OnDeleteButtonClick(sender, e);
        }

        /// <summary>
        /// 生效按钮的执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPublishButtonClick(object sender, RoutedEventArgs e)
        {
            this.actionType = "publish";
            base.OnModifyButtonClick("publish", "生效", sender, e);
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "设备变动-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.type"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.engineering"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.department"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.equipment"));
            AddEquipmentChange AddEquipmentChange = new AddEquipmentChange(_dialogType);//构造一个新的修改页面
            AddEquipmentChange.EIDataContext = new Platform.Core.UI.EIDataContext();
            AddEquipmentChange.EIDataContext.Update(_eIInfo);

            if ("update".Equals(_dialogType))
            {
                if ("领用".Equals(this.grid.CheckedEIRows[0]["fdType"]) ||
                    "启封".Equals(this.grid.CheckedEIRows[0]["fdType"]) ||
                    "移交".Equals(this.grid.CheckedEIRows[0]["fdType"]))
                {
                    AddEquipmentChange.fdNewDepartmentId.eLabel = "*现所在部门";
                    AddEquipmentChange.fdNewDepartmentId.eRegex = "^.+$";
                    AddEquipmentChange.fdNewDepartmentId.eValidateErrorPrompt = "不能为空！";
                }

            }

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = AddEquipmentChange;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 删除,修改前的判断
        /// </summary>
        /// <param name="rowList"></param>
        /// <returns></returns>
        public override bool BeforeMethod(List<EIRow> _eIRows)
        {
            bool Flag = true;
            if ("update".Equals(this.actionType))
            {
                if (!"草拟".Equals(_eIRows[0]["fdStatus"].ToString()))
                {
                    base.OpenEFWindowAlert("只能对草拟的记录进行修改！", true);
                    Flag = false;
                }
            }
            else if ("delete".Equals(this.actionType))
            {
                foreach (var row in _eIRows)
                {
                    if (!"草拟".Equals(row["fdStatus"].ToString()))
                    {
                        base.OpenEFWindowAlert("只能对草拟的记录进行删除！", true);
                        Flag = false;
                        break;
                    }
                }
            }
            else if ("publish".Equals(this.actionType))
            {
                if (_eIRows.Count > 1)
                {
                     base.OpenEFWindowAlert("一次只能生效一条记录！", true);
                     Flag = false;
                }
                else
                {
                    if (!"草拟".Equals(_eIRows[0]["fdStatus"].ToString()))
                    {
                        base.OpenEFWindowAlert("记录已经生效！", true);
                        Flag = false;
                    }
                }
            }
            return Flag;
        }
    }
}
